package com.example.canvasdemo_02;

import static android.content.Context.WIFI_SERVICE;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.example.canvasdemo_02.Beans.WifiStream;
import com.example.canvasdemo_02.MyThread.NetLocationThread;
import com.example.canvasdemo_02.MyThread.NetLoginThread;
import com.example.canvasdemo_02.WiFi.MyWifiManager;
import com.example.canvasdemo_02.databinding.FragmentSecondBinding;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class SecondFragment extends Fragment {

    String result="";
    private FragmentSecondBinding binding;
    private static final int PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION = 1;
    Canvas canvas=new Canvas();
    MyWifiManager wifiManager;
    List<WifiStream> wifiList;
    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        View ret = inflater.inflate(R.layout.fragment_second,container,false);
        //Log.i("Tag","Msg-hello@");
        binding = FragmentSecondBinding.inflate(inflater, container, false);
        WifiManager wifimanager= (WifiManager) ret.getContext().getApplicationContext().getSystemService(WIFI_SERVICE);//获取wifi服务
        wifiManager = new MyWifiManager(wifimanager);
        return binding.getRoot();
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.buttonSecond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                binding.buttonSecond.setEnabled(false);
                int point_x=(int)(0+Math.random()*(MainActivity.screen_widths));
                int point_y=(int)(0+Math.random()*((int)MainActivity.screen_height*0.9221));
                binding.paintBoard.setPoint(new Point(point_x,point_y),Color.argb(0xff,0xff,0x00,0x00));

                //获取wifi表
                wifiList = wifiManager.getWifiList();
                //将列表打包成json数据
                JSONObject wifiParams = new JSONObject();
                for(int i=0;i<wifiList.size();i++){
                    if(!wifiList.get(i).isEmpty()){
                        try {
                            wifiParams.put(wifiList.get(i).getWifiStream_mac(),wifiList.get(i).getWifiStream_rssi());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
                JSONObject params = new JSONObject();
                try {
                    params.put("currentUser",FirstFragment.CurrentAccount);
                    params.put("wifi_rssi",wifiParams);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //将json数据包发送出去
                NetLocationThread loginThread = new NetLocationThread(params);
                loginThread.start();
                try {
                    loginThread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                result=loginThread.getResult();
                //解析返回的数据

                //将数据当中的位置信息显示在屏幕上

                binding.buttonSecond.setEnabled(true);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}